﻿/**********************************************************\
*  TinyLib - AnimatedSprite                                *
*                                                          *
*  Animates a sprite from a sprite sheet.                  *
*                                                          *
* (c) Jens Berglind                                        *
*                                                          *
* Last edited by Jens Berglind 25-06-2012                  *
\**********************************************************/ 

using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AALib.Sprite
{
    public class AnimatedSprite : Sprite
    {
        #region Fields

        public Dictionary<string, Strip> m_strips = new Dictionary<string, Strip>();
        public Vector2 m_frameSize;
        public string m_strCurrentAnimation = "";

        bool m_bIsAnimating = true;
        Vector2 m_velocity;

        #endregion

        #region Properties

        public Color Color
        {
            get { return m_color; }
            set { m_color = value; }
        }

        public bool BoolIsAnimating
        {
            get { return m_bIsAnimating; }
            set { m_bIsAnimating = value; }
        }
        /*
        public int IntWidth
        {
            get { return strips[strCurrentAnimation].IntFrameWidth; }
        }

        public int IntHeight
        {
            get { return strips[strCurrentAnimation].IntFrameHeight; }
        }
        */
        /*
        public float FloatSpeed
        {
            get { return fSpeed; }
            set { fSpeed = MathHelper.Clamp(fSpeed, 1.0f, 400.0f); }
        }
        */
        public Vector2 Position
        {
            get { return m_position; }
            set
            {
                m_position = value;
                if (m_velocity != Vector2.Zero)
                { m_velocity.Normalize(); }
            }
        }

        public string Animation
        {
            get { return m_strCurrentAnimation; }
            set
            {
                m_strips[m_strCurrentAnimation].Reset();
                m_strCurrentAnimation = value;
            }
        }

        public Vector2 FrameSize
        {
            get { return m_frameSize; }
            set { m_frameSize = value; }
        }

        #endregion

        #region Init

        public AnimatedSprite(SpriteBatch p_spriteBatch, Texture2D p_texture, Game p_game)
            : base(p_spriteBatch, p_texture, p_game)
        {
            m_spritebatch = p_spriteBatch;
        }

        #endregion

        #region Methods

        public void AddStrip(string p_id, int p_numberOfFrames, int p_timeOnChange)
        {
            Strip strip = new Strip();
            strip.timeOnChange = p_timeOnChange;
            int frameWidth = m_texture.Width / (int)m_frameSize.X;
            int frameHeight = m_texture.Height / (int)m_frameSize.Y;

            Rectangle rectangle;
            rectangle.Y = 0 + (frameHeight * m_strips.Count);
            rectangle.Width = frameWidth;
            rectangle.Height = frameHeight;
            for (int i = 0; i < p_numberOfFrames; i++)
            {
                rectangle.X = i * frameWidth;
                strip.AddRectangle(rectangle);
            }

            m_strips[p_id] = strip;
            m_strCurrentAnimation = p_id;
        }

        public override void Draw(GameTime p_gameTime)
        {
            m_spritebatch.Draw(m_texture, m_position, m_strips[m_strCurrentAnimation].GetRectangle(p_gameTime), m_color, m_rotation, m_origin, m_scale, m_effect, DrawOrder);
        }
        
        public void DrawDeath(GameTime p_gameTime)
        {
            int rects = m_strips[m_strCurrentAnimation].rectangles.Count;

            m_spritebatch.Draw(m_texture, m_position, m_strips[m_strCurrentAnimation].rectangles[(rects - 1)], m_color, m_rotation, m_origin, m_scale, m_effect, DrawOrder);
        }
        
        #endregion
    }
}
